package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.mapper.admin.DishMapper;
import com.sky.mapper.admin.SetmealMapper;
import com.sky.mapper.user.ShoppingCartMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.Setmeal;
import com.sky.pojo.ShoppingCart;
import com.sky.service.user.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public void addSCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();

        Long userId = BaseContext.getCurrentId();

        shoppingCart.setUserId(userId);
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        shoppingCart.setCreateTime(LocalDateTime.now());
        ShoppingCart existCart = shoppingCartMapper.findByCondition(userId,shoppingCartDTO);
        if (existCart!=null){
            log.debug("66666");
            shoppingCartMapper.updateSCart(existCart.getNumber()+1,existCart.getId());
            return;
        }
        shoppingCart.setNumber(1);
        if (shoppingCartDTO.getDishId()!=null){
            //有菜品id
            shoppingCart.setDishId(shoppingCartDTO.getDishId());
            Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else {
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }
        shoppingCartMapper.insertSC(shoppingCart);
    }

    @Override
    public List<ShoppingCart> findAll() {
        return shoppingCartMapper.findbyUserId(BaseContext.getCurrentId());
    }

    @Override
    public void cleanAll() {
        shoppingCartMapper.cleanAll(BaseContext.getCurrentId());
    }

    @Override
    public void subSCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        ShoppingCart existCart = shoppingCartMapper.findByCondition(userId,shoppingCartDTO);
        if (existCart.getNumber()>1){
            shoppingCartMapper.updateSCart(existCart.getNumber()-1,existCart.getId());
        }else {
            shoppingCartMapper.delById(existCart.getId());
        }
    }
}
